package com.example.easyutils;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.util.DialogLoad;
import com.example.easyutils.util.MyTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.ZLoadingView;
import com.zyao89.view.zloading.Z_TYPE;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class HistoryToday extends BaseActivity {
    private TextView historyStr,todayStr;
    private Map historyTodayMap;
    private static final int GET_HISTORY_TODAY = 1,GET_HISTORY_TODAY_ERROR = 2,WARN_CODE = 3;
//    ZLoadingDialog dialog = new ZLoadingDialog(HistoryToday.this);//加载页面动画
    DialogLoad dialogLoad;
    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            dialogLoad.dialogCancel(); //关闭加载
            switch (msg.what){
                case GET_HISTORY_TODAY:
                    String contentStr = null;
                    Log.d("成功回调",historyTodayMap+"");
                    String str = historyTodayMap.get("code").toString();
                    contentStr = historyTodayMap.get("content").toString();
                    if(!str.equals("200") || contentStr==null) {
                        historyStr.setText("出错啦");
                        return;
                    }
                    todayStr.setText("今天日期："+historyTodayMap.get("day").toString());
                    String historyEvent = historyTodayMap.get("content").toString()
                            .replace("[","").replace("]","")
                            .replaceAll(",",";\n \n");
//                    Log.d("替换后",historyEvent);
                    historyStr.setText("历史事件：\n \n"+historyEvent);

                    break;
                case GET_HISTORY_TODAY_ERROR:
                    historyStr.setText("接口出错");
                    break;
                case WARN_CODE:
                    historyStr.setText("出错，请重试");
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        historyStr = (TextView)findViewById(R.id.historyDate);
        todayStr = (TextView)findViewById(R.id.todayDate);
        dialogLoad = new DialogLoad(HistoryToday.this);
        getHistoryToday();

        dialogLoad.dialogLoaded("加载中...");

    }

    private void getHistoryToday(){//历史上的今天
        HttpGetRequest.sendOkHttpGetRequst("http://api.63code.com/history/api.php?format=json",
                new okhttp3.Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(GET_HISTORY_TODAY_ERROR);
                        Log.d("error","出错");
                    }

                    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String responseData = response.body().string();
                        Log.d("成功",responseData);
                        Gson gson = new GsonBuilder()
                                .registerTypeAdapter(new TypeToken<Map<String, Object>>(){

                                }.getType(),new MyTypeAdapter()).create();
                        Map<String,Object> dataDetail = gson.fromJson(responseData,new TypeToken<Map<String,Object>>(){

                        }.getType());

                        String code = (String) dataDetail.get("code");
                        List list = null;
                        list = (List) dataDetail.get("content");
                        if(code=="200" && list!=null){
                            historyTodayMap = new HashMap();
                            historyTodayMap = dataDetail;
                            handler.sendEmptyMessage(GET_HISTORY_TODAY);
                        }else{
                            handler.sendEmptyMessage(WARN_CODE);
                        }


//                        Log.d("成功1",dataDetail+"");
                    }
                });
    }
}
